const getOrderList = () => {

  try {
    if (localStorage.orderList == undefined) {
      localStorage.orderList = []

    }
    return JSON.parse(localStorage.orderList) || []
  } catch (e) {
    return [];
  }
}
const GetNewOrderID = () => {
  try {

    if (localStorage.orderList == undefined) {

      return 1

    }
    let orderList = JSON.parse(localStorage.orderList) || [];
    if (orderList.length > 0) {
      return orderList[orderList.length - 1].orderId + 1;
    }
  } catch (e) {
    return 1;
  }

}
const SetOrderList = (state) => {
  if (state.orderList != undefined) {
    localStorage.orderList = JSON.stringify(state.orderList);

  }

}
// 不要忘记把state, mutations等暴露出去。
export default {
  namespaced: true,
  state: {
    orderList: getOrderList(),
    newOrderId: GetNewOrderID()
  },
  mutations: {
    createOrder(state, payload) {
      state.orderList.push(payload.model);
      SetOrderList(state);
    },
    deleteOrder(state, payload) {

      let index = state.orderList.findIndex((item) => {
        return item.orderId == payload.orderId
      })

      if (index > -1) {
        state.orderList.splice(index, 1);
        
        SetOrderList(state);

      }
    },


    //更新订单信息
    setOrderInfo(state, payload) {

      var index = state.orderList.findIndex((item) => {
        return item.orderId == payload.orderId
      })
      if (index > -1) {
        let orderModel = state.orderList[index];
        orderModel.order_status = payload.order_status;
        orderModel.addressId = payload.addressId;
        state.orderList[index] = orderModel;
        SetOrderList(state);
      }

    }


  },
  actions: {

  },
  getters: {

  }

}
